package cz.schovjan.proj.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.schovjan.proj.dto.Band;
import cz.schovjan.proj.dto.Concert;
import cz.schovjan.proj.dto.Member;
import cz.schovjan.proj.dto.Song;
import cz.schovjan.proj.service.IBand;
import cz.schovjan.proj.service.IMember;
import cz.schovjan.proj.service.ISong;
import cz.schovjan.proj.service.IConcert;

/**
 * Kontroler pro pridavani kapel, clenu, koncertu.
 * @author schovjan
 *
 */
@Controller
@RequestMapping("/add")
@SessionAttributes({"user"}) //user se musi uvest taky, jinak se nepouzije
public class AddController extends Base {
	
	/** stranka s pridavanim zaznamu do DB */
	private static final String ADD = "add";

	/** Service pro kapely. */
	@Autowired
	private IBand bands;
	
	/** Service pro cleny.  */
	@Autowired
	private IMember members;
	
	/** Service pro pisne. */
	@Autowired
	private ISong songs;
	
	/** Service pro koncerty. */
	@Autowired
	private IConcert concerts;
	
	@ModelAttribute("band")
	public Band createBand() {
		return new Band();
	}
	
	@ModelAttribute("member")
	public Member createMember() {
		return new Member();
	}
	
	@ModelAttribute("song")
	public Song createSong() {
		return new Song();
	}
	
	@ModelAttribute("concert")
	public Concert createConcert() {
		return new Concert();
	}
	

	/////////////////
	//OBSLUZNE FUKNCE ------------------------------------------------------------
	/////////////////
	
	
	/**
	 * Zobrazeni pridavani kapely.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/band")
	public String addBand(ModelMap model) {
		model.addAttribute("addBand", true);
		
		return ADD;
	}
	
	
	/**
	 * Zobrazeni pridavani clena.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/member")
	public String addMember(ModelMap model) {
		model.addAttribute("addMember", true);
		
		return ADD;
	}

	
	/**
	 * Zobrazeni pridavani pisne.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/song")
	public String addSong(ModelMap model) {
		model.addAttribute("addSong", true);
		
		return ADD;
	}
	
	
	/**
	 * Zobrazeni pridavani koncertu.
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/concert")
	public String addConcert(ModelMap model) {
		model.addAttribute("addConcert", true);
		
		return ADD;
	}

	
	/**
	 * Pridani kapely.
	 * @param band
	 * @param result
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/bandSubmit")
	public String bandSubmit(@Valid Band band, BindingResult result, ModelMap model) {
		model.addAttribute("addBand", true);
		
		if (result.hasErrors()) {
			return ADD;
		}
		bands.add(band.getName(),
					band.getEmail(), 
					band.getDescription(), 
					band.getCreation(), 
					band.getBreakup());
		band = new Band();
		model.addAttribute("message", MSG_BAND_SAVE);
		
		return ADD;
	}
	
	
	/**
	 * Pridani clena.
	 * @param member
	 * @param result
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/memberSubmit")
	public String memberSubmit(@Valid Member member, BindingResult result, ModelMap model) {
		model.addAttribute("addMember", true);
		
		if (member.getNick().isEmpty() && member.getName().isEmpty() && member.getSurname().isEmpty()) {
			result.addError(new ObjectError("clenBezJmena", MSG_VALID_MEMBER));
		}
		
		if (result.hasErrors()) {
			return ADD;
		}
		members.add(member.getNick(),
					member.getName(),
					member.getSurname(),
					member.getEmail(),
					member.getPhone(),
					member.getDateOfBirth(),
					member.getJob());
		member = new Member();
		model.addAttribute("message", MSG_MEMBER_SAVE);
		
		return ADD;
	}
	
	
	/**
	 * Pridani pisne.
	 * @param song
	 * @param result
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/songSubmit")
	public String songSubmit(@Valid Song song, BindingResult result, ModelMap model) {
		model.addAttribute("addSong", true);
		
		if (song.getNameCs().isEmpty() && song.getNameOriginal().isEmpty()) {
			result.addError(new ObjectError("pisenBezNazvu", MSG_VALID_SONG));
		}
		
		if (result.hasErrors()) {
			return ADD;
		}
		songs.add(song.getNameCs(),
					song.getNameOriginal(),
					song.getAuthor(),
					song.getArrange(),
					song.getDescription());
		song = new Song();
		model.addAttribute("message", MSG_SONG_SAVE);
		
		return ADD;
	}
	
	
	/**
	 * Pridani koncertu.
	 * @param concert
	 * @param result
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/concertSubmit")
	public String concertSubmit(@Valid Concert concert, BindingResult result, ModelMap model) {
		model.addAttribute("addConcert", true);
		
		if (result.hasErrors()) {
			return ADD;
		}
		concerts.add(concert.getPlace(),
						concert.getDateTime(),
						concert.getDescription());
		model.addAttribute("message", MSG_CONCERT_SAVE);
		
		return ADD;
	}
	
}
